package com.bluesky.jwf.component.window;

import com.bluesky.javawebbrowser.domain.html.tags.Tag;
import com.bluesky.jwf.ComponentFactory;
import com.bluesky.jwf.component.Component;
import com.bluesky.jwf.component.event.AbstractEventHandler;
import com.bluesky.jwf.component.event.EventHandler;
import com.bluesky.jwf.component.fake.Linker;
import com.bluesky.jwf.component.tab.Tab;
import com.bluesky.jwf.component.tab.Tabs;

/**
 * it contains a menu bar, child window area, status bar.
 * 
 * @author hz00260
 * 
 */
public class ClassicalMdiParentWindow extends MainWindow {

	protected Tag menuBar;
	protected Tag childWindowArea;

	protected Tabs childWindowTabs;

	@Override
	public void init(String html) {

		super.init(html);

		menuBar = bodyTag.loadByJwfClass("menuBar");
		childWindowArea = bodyTag.loadByJwfClass("childWindowArea");

		childWindowTabs = (Tabs) ComponentFactory.getInstance()
				.createComponent(Tabs.class.getSimpleName());

		childWindowArea.removeAllChildren();
		childWindowArea.addChild(childWindowTabs);
	}

	@Override
	public void addChildWindow(final Window child) {

		super.addChildWindow(child);

		EventHandler closeHandler = new AbstractEventHandler() {
			@Override
			public void handle(Component target, Object parameter) {
				removeChildWindow(child);

				if (child.getCloseHandler() != null)
					child.getCloseHandler().handle(child, null, null);

			}
		};

		Linker linker = new Linker(child.getBodyTag());
		Tab tab = new Tab(child.getTitle(), linker);
		tab.setQueryCloseHandler(child.getQueryCloseHandler());
		tab.setCloseHandler(closeHandler);
		childWindowTabs.addTab(tab);
	}

}
